//--------------------------------------------------------//
//Filename:excutedb.js
//Create Time:2020年02月07日 星期五 01时17分51秒
//Description: 处理数据库的一些操作
const uuid = require('uuid');
const m = require('../db/models');
const {managermodel:manager,wordmodel:word,etymamodel:etyma_m} = m;

var showField = {_id:0,word:1,recordtime:1,etymas:1,explanations:1,phrase:1};
var populateConfig = {path:'etymas',select:'etyma -_id'};

//获取单词列表 get
exports.getwords = async function(ctx,next){
	let {skip,limit}= ctx.params;
	skip = parseInt(skip)?parseInt(skip):0;
	limit = parseInt(limit)?parseInt(limit):10;
	let count = await word.count();
	let words = await word.find().skip(skip).limit(limit);
	ctx.response.body = {words,count};
}

//获取词根列表 get
exports.getetymas = async function(ctx,next){
	let {skip,limit}= ctx.params;
	skip = parseInt(skip)?parseInt(skip):0;
	limit = parseInt(limit)?parseInt(limit):10;
	let count = await etyma_m.count();
	let etymas = await etyma_m.find().skip(skip).limit(limit);
	ctx.response.body = {etymas,count};
}

//查询单词
exports.searchwords = async function(ctx,next){
	if(ctx.params.letters){
		let letters = ctx.params.letters;
		let rex = new RegExp(letters+'\\w*');
		let words = await word.find({word:rex});
		let count = words.length;
		ctx.response.body = {words,count};
	}else{
		ctx.response.body = {words:[],count:0,message:'没有传入参数'}	
	}
}

//删除单词
exports.deleteword = async function(ctx,next){
	let _id = ctx.params.wordid;
	let obj = await word.deleteOne({_id:_id});
	ctx.response.body ={deletewordid:_id,res:obj};
}

//删除词根
exports.deleteetyma= async function(ctx,next){
	let _id = ctx.params.etymaid;
	let obj = await etyma_m.deleteOne({_id:_id});
	ctx.response.body={deleteetymaid:_id,res:obj,message:'删除词根成功',codstatus:201};
}

//整体编辑单词 post方法
exports.updateword = async function(ctx,next){
	let data = ctx.request.body;
	let id = data._id;
	 
	let check = await word.findOne({word:data.word,_id:{"$nin":[id]}});
	console.log(check);
	if(check){
		console.log('the word has existed.');
		console.log(check);
		ctx.response.body = {statuscode:409,msg:'不能修改为已经存在的单词'};
	}else{
		let obj = await word.findByIdAndUpdate(id,data);
		obj.statuscode = 200;
		obj.msg = "单词已经成功编辑";
		ctx.response.body = obj;
	}
}

//整体编辑词根 post方法
exports.updateetyma = async function(ctx,next){
	let data = ctx.request.body;
	let id = data._id;
	 
	let check = await etyma_m.findOne({etyma:data.etyma,_id:{"$nin":[id]}});
	console.log(check);
	if(check){
		console.log('the etyma has existed.');
		console.log(check);
		ctx.response.body = {statuscode:409,message:'不能修改为已经存在的词根'};
	}else{
		let obj = await etyma_m.findByIdAndUpdate(id,data);
		ctx.response.body = {data:obj,statuscode:208,message:'词根已经更新成功'};
	}
}
//增加单词学习次数 get 方法
exports.addclasstime = async function(ctx,next){
	let id = ctx.params.id;
	let res = await word.findByIdAndUpdate(id,{"$inc":{"classtime":1}});
	ctx.response.body = res;
}


exports.queryword = async function(ctx,next){
	let enword = ctx.params.word;
	let obj =await word.find({'word':enword},showField).populate(populateConfig);
	ctx.response.body = obj;
}

//添加词根 post方法
exports.addnewetyma = async function(ctx,next){
	let data = ctx.request.body;
	let etyma = data.etyma;
	let obj = await etyma_m.findOne({etyma:etyma});
	console.log(obj);
	console.log(data);	
	if(obj){
		console.log(obj);
		console.log('etyma has existed');
		ctx.response.body = 
			{message:'etyma has existd',
			 codestatus:408,
			 data:obj};
	}else{
		let res = await new etyma_m(data).save();		
		ctx.response.body = {codestatus:280,message:'提交新词根成功',data:res};
	}
}

exports.queryetyma = async function(ctx,next){
	let _etyma = ctx.params.etyma;
	let obj = await etyma.findOne({'etyma':_etyma});
	ctx.response.body = obj;
}

exports.addnewword = async function(ctx,next){
	let data = ctx.request.body;
	console.log(word);
	console.log(data);
	let isword= await word.find({word:data.word});
	console.log(isword[0]);
	console.log(isword.length);
	if(isword.length>0){
		ctx.response.body ={codestatus:405,message:'单词已存在'};	
	}else{
		let res = await new word(data).save();	
		res.codestatus = 200;
		console.log(res);
		ctx.response.body = {codestatus:280,message:'单词已存储'};
	}
}

const findmanageronly = async function({username,userpassword}){
	let info = await manager.findOne({username:username,userpassword:userpassword});	
	let resobj =null;
	if(info){
		resobj = {statuscode:200,msg:'找到用户',data:info};
	}else{
		resobj = {statuscode:409,msg:'没有找到用户，请检查密码和用户名'};
	}
	return resobj;
}

exports.findmanageronly = findmanageronly;
exports.findmanager = async function(ctx,next){
	let {username,userpassword} = ctx.request.body;
	let resobj = await findmanageronly({username:username,userpassword:userpassword});
	ctx.response.body=resobj;
}

exports.addmanager = async function(ctx,next){
	let {username,userpassword} = ctx.request.body;
	let uid = uuid.v1();
	let existuser = await manager.find({username:username});	
	let resobj=null;
	if(existuser.length>0){
		resobj = {statuscode:'407',msg:'用户名已经存在'}	
	}else{
		let data = {
			username:username,
			userpassword:userpassword,
			uid:uid
		};
		if(ctx.request.body.powermask)
			data.powermask = ctx.request.body.powermask;
		let res = await new manager(data).save();
		resobj = {statuscode:'200',msg:res};	
	}
	ctx.response.body = resobj;
}
